package cn.sangkf.litemall.common.valid.annoation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author : sangkf
 * @package : cn.sangkf.litemall.common.valid.listValues.ListValuesConstraintValidator
 * @date : 2020/12/26 下午9:48 星期六
 * @description : TODO
 * @version: 1.0
 * <p>Copyright: Copyright (c) 2020</p>
 */
public class ListValuesConstraintValidator implements ConstraintValidator<ListValues, Integer> {

    private Set<Integer> set = new HashSet<Integer>();

    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }

    public void initialize(ListValues constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        for (int i : values) {
            set.add(i);
        }
    }
}
